package com.bus.common.captcha;

import java.awt.Color;
import java.awt.Font;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;
import com.octo.captcha.component.image.color.SingleColorGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.DecoratedRandomTextPaster;
import com.octo.captcha.component.image.textpaster.textdecorator.BaffleTextDecorator;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.engine.GenericCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
import com.octo.captcha.service.multitype.GenericManageableCaptchaService;

@Configuration
public class ConfigJcaptchaServlet {

	@Bean
	public GenericManageableCaptchaService captchaService() {
		return new GenericManageableCaptchaService(imageEngine(), 180, 100000,
				75000);
	}

	@Bean
	public GenericCaptchaEngine imageEngine() {
		GimpyFactory factory[] = { captchaFactory() };
		return new GenericCaptchaEngine(factory);
	}

	@Bean
	public GimpyFactory captchaFactory() {
		return new GimpyFactory(wordgen(), wordtoimage());
	}

	@Bean
	public RandomWordGenerator wordgen() {
		return new RandomWordGenerator(
				"aabbccddeefgghhkkmnnooppqqsstuuvvwxxyyzz");
	}

	@Bean
	public ComposedWordToImage wordtoimage() {
		return new ComposedWordToImage(fontGenRandom(), backGenUni(),
				decoratedPaster());
	}

	@Bean
	public RandomFontGenerator fontGenRandom() {
		Font font[] = { new Font("Arial", 0, 32) };
		RandomFontGenerator fontGenRandom = new RandomFontGenerator(26, 34,
				font);
		return fontGenRandom;
	}

	@Bean
	public UniColorBackgroundGenerator backGenUni() {
		return new UniColorBackgroundGenerator(110, 50);
	}

	@Bean
	public DecoratedRandomTextPaster decoratedPaster() {
		BaffleTextDecorator baffleDecorator[] = {};
		return new DecoratedRandomTextPaster(4, 4, colorGen(), baffleDecorator);
	}

	@Bean
	public SingleColorGenerator colorGen() {
		return new SingleColorGenerator(colorBlack());
	}

	@Bean
	public Color colorBlack() {
		return new Color(50, 50, 50);
	}
}
